package com.ztools.pc.service.impl.store;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztools.common.model.pc.store.Commodity;
import com.ztools.common.model.pc.store.OnlineStock;
import com.ztools.common.model.pc.store.StoreStock;
import com.ztools.common.util.common.PageUtil;
import com.ztools.common.util.pc.DictUtil;
import com.ztools.pc.mapper.store.StoreStockMapper;
import com.ztools.pc.service.store.CommodityService;
import com.ztools.pc.service.store.OnlineStockService;
import com.ztools.pc.service.store.StoreStockService;
import com.ztools.pc.service.sys.DictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author seeTools
 * @since 2023-05-27
 */
@Service
public class StoreStockServiceImpl extends ServiceImpl<StoreStockMapper, StoreStock> implements StoreStockService {

    @Resource
    private CommodityService commodityService;
    @Resource
    private DictService dictService;
    @Resource
    private OnlineStockService onlineStockService;

    /**
     * 分页查询
     *
     * @param pageUtil 分页数据
     * @return 对象
     */
    @Override
    public Page<StoreStock> findList(PageUtil<StoreStock> pageUtil) {
        List<OrderItem> orders = new ArrayList<>();
        QueryWrapper<StoreStock> queryWrapper = getQueryWrapper(pageUtil.getParams());
        Page<StoreStock> pages = new Page<>(pageUtil.getPage(), pageUtil.getSize());
        // 排序数据  StrUtil.toUnderlineCase huTools将驼峰装换为下划线格式
        orders.add(new OrderItem(StrUtil.toUnderlineCase(pageUtil.getSortName()), "asc".equals(pageUtil.getSortOrder())));
        pages.setOrders(orders);
        Page<StoreStock> page = page(pages, queryWrapper);
        dictionary(page.getRecords());
        return page;
    }

    @Override
    public List<StoreStock> findByObject(StoreStock storeStock) {
        QueryWrapper<StoreStock> queryWrapper = new QueryWrapper<>(storeStock);
        return list(queryWrapper);
    }

    @Override
    public boolean update(StoreStock storeStock) {
        boolean b = false;
        // 修改前先获取，在删除（乐观锁）
        StoreStock byId = getById(storeStock);
        b = updateById(storeStock);
        return b;
    }

    @Override
    public boolean updateQuantity(StoreStock storeStock) {
        UpdateWrapper<StoreStock> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(StoreStock::getCommodityId, storeStock.getCommodityId())
                .set(StoreStock::getQuantity, storeStock.getQuantity());
        return update(wrapper);
    }

    @Override
    public boolean delete(StoreStock storeStock) {
        boolean b = false;
        StoreStock byId = getById(storeStock);
        b = removeById(storeStock);
        return b;
    }

    @Override
    public boolean updateSalePrice(StoreStock storeStock) {
        StoreStock byId = getById(storeStock);
        boolean b = update(storeStock);
        if (b) {
            // 修改商品价格
            Commodity commodity = new Commodity();
            commodity.setId(storeStock.getCommodityId());
            commodity.setSalePrice(storeStock.getSalePrice());
            boolean cb = commodityService.update(commodity);
            // 修改线上商城的价格
            UpdateWrapper<OnlineStock> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(OnlineStock::getCommodityId, storeStock.getCommodityId())
                    .set(OnlineStock::getSalePrice, storeStock.getSalePrice());
            return onlineStockService.update(updateWrapper);
        }
        return false;
    }

    @Override
    public List<StoreStock> findBy(String fieldName, Object value) {
        QueryWrapper<StoreStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(fieldName, value);
        return list(queryWrapper);
    }

    public QueryWrapper<StoreStock> getQueryWrapper(StoreStock storeStock) {
        QueryWrapper<StoreStock> queryWrapper = new QueryWrapper<>();
        if (storeStock != null) {

        }
        return queryWrapper;
    }

    /**
     * 字典翻译
     *
     * @param storeStockList 对象集合
     */
    public void dictionary(List<StoreStock> storeStockList) {
        for (StoreStock storeStock : storeStockList) {
            dictionary(storeStock);
        }
    }

    public void dictionary(StoreStock storeStock) {
        if (storeStock != null) {
            if (storeStock.getCommodityId() != null) {
                Commodity commodity = commodityService.getById(storeStock.getCommodityId());
                storeStock.setCommodityName(commodity.getName());
                storeStock.setUnit(dictService.findByTypeCode(DictUtil.UNIT, commodity.getUnit()));
            }
        }
    }

}

